#include "FrameLessHelper.h"
#include "framelesshelperprivate.h"
#include "widgetdata.h"

#include <QRect>
#include <QRubberBand>
#include <QMouseEvent>
#include <QHoverEvent>
#include <QApplication>


/*****FrameLessHelper*****/
FrameLessHelper::FrameLessHelper(QObject *parent)
    : QObject(parent),
      d(new FrameLessHelperPrivate())
{
    d->isWidgetMovable = true;
    d->isWidgetResizable = true;
    d->isRubberBandOnResize = false;
    d->isRubberBandOnMove = false;
}

FrameLessHelper::~FrameLessHelper()
{
    QList<QWidget*> keys = d->widgetDataHash.keys();
    int size = keys.size();
    for (int i = 0; i < size; ++i)
    {
        delete d->widgetDataHash.take(keys[i]);
    }

    delete d;
}

bool FrameLessHelper::eventFilter(QObject *obj, QEvent *event)
{
    switch (event->type())
    {
    case QEvent::MouseMove:
    case QEvent::HoverMove:
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
    case QEvent::Leave:
    {
        WidgetData *data = d->widgetDataHash.value(static_cast<QWidget*>(obj));
        if (data)
        {
            data->handleWidgetEvent(event);
            return true;
        }
    }
    }
    return QObject::eventFilter(obj, event);
}

void FrameLessHelper::activateOn(QWidget *topLevelWidget)
{
    if (!d->widgetDataHash.contains(topLevelWidget))
    {
        WidgetData *data = new WidgetData(d, topLevelWidget);
        d->widgetDataHash.insert(topLevelWidget, data);

        topLevelWidget->installEventFilter(this);
    }
}

void FrameLessHelper::removeFrom(QWidget *topLevelWidget)
{
    WidgetData *data = d->widgetDataHash.take(topLevelWidget);
    if (data)
    {
        topLevelWidget->removeEventFilter(this);
        delete data;
    }
}

void FrameLessHelper::setRubberBandOnMove(bool movable)
{
    d->isRubberBandOnMove = movable;
    QList<WidgetData*> list = d->widgetDataHash.values();
    foreach (WidgetData *data, list)
    {
        data->updateRubberBandStatus();
    }
}

void FrameLessHelper::setWidgetMovable(bool movable)
{
    d->isWidgetMovable = movable;
}

void FrameLessHelper::setWidgetResizable(bool resizable)
{
    d->isWidgetResizable = resizable;
}

void FrameLessHelper::setRubberBandOnResize(bool resizable)
{
    d->isRubberBandOnResize = resizable;
    QList<WidgetData*> list = d->widgetDataHash.values();
    foreach (WidgetData *data, list)
    {
        data->updateRubberBandStatus();
    }
}

void FrameLessHelper::setBorderWidth(uint width)
{
    if (width > 0)
    {
        CursorPosCalculator::borderWidth = width;
    }
}

void FrameLessHelper::setTitleHeight(uint height)
{
    if (height > 0)
    {
        CursorPosCalculator::titleHeight = height;
    }
}

bool FrameLessHelper::getWidgetMovable()
{
    return d->isWidgetMovable;
}

bool FrameLessHelper::getWidgetResizable()
{
    return d->isWidgetResizable;
}

bool FrameLessHelper::getRubberBandOnMove()
{
    return d->isRubberBandOnMove;
}

bool FrameLessHelper::getRubberBandOnResisze()
{
    return d->isRubberBandOnResize;
}

uint FrameLessHelper::getBorderWidth()
{
    return CursorPosCalculator::borderWidth;
}

uint FrameLessHelper::getTitleHeight()
{
    return CursorPosCalculator::titleHeight;
}
